// pages/zhlShop/zhlShop.js
import * as API from '../../../api/pedestal';
import * as util from '../../../utils/util';
let app = getApp ();

Page({

  /**
   * 页面的初始数据
   */
  
data: {
  cateItems:[],
  isShowModal:false,
  isImgModal: false,
  imgCode: '',
  id: null,
  pedestalName: null,
  pedestalTypeName: null,
  opening: false
},
/**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getTypeList()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    this.getTypeList()
  },
  getTypeList(){
    // API.getProductType().then(res => {
      // this.setData({
      //   cateItems: res.data||[],
      //   curNav: res.data[0].id
      // })
      API.getCommodity().then(res => {
        let dataList = res.data || []
        this.setData({
          cateItems: dataList
        })
      })
    // })
  },
  toProduct(options){
    var url = options.currentTarget.dataset.url;
    var id = options.currentTarget.dataset.id;
    url = id?(url+'?id='+id):url
      util.preventMoreClick (this, () => {
        wx.navigateTo ({
          url: url,
        });
      });
  },
  openModal(e){
    let id= e.currentTarget.dataset.id
    let pedestalname= e.currentTarget.dataset.pedestalname
    let pedestaltypename= e.currentTarget.dataset.pedestaltypename
    let opening= e.currentTarget.dataset.opening
    this.setData({
      isShowModal: true,
      id:id,
      pedestalName: pedestalname,
      pedestalTypeName: pedestaltypename,
      opening: opening
    })
  },
  hideMarker: function () {
    this.setData({
      isShowModal: false
    })
  },
  // code
  hideImgMarker(){
    this.setData({
      isImgModal: false
    })
  },
  switchOpen(){
    let postBody = { opening: !this.data.opening }
    API.editCommodity(this.data.id, postBody).then(res => {
      this.hideMarker()
      this.getTypeList()
      // wx.showToast({
      //   icon: 'success',
      //   title: '修改成功',
      //   duration: 1500
      // });
    }, err => {
      this.hideMarker()
      // wx.showToast({
      //   icon: 'none',
      //   title: err || '修改失败'
      // });
    })
  },
  homePageGoto: function (options) {
    this.hideMarker()
    var url = options.currentTarget.dataset.url+'?id='+this.data.id;
    util.preventMoreClick (this, () => {
      wx.navigateTo ({
        url: url
      });
    });
  },
  deleteModal(e){
    let id= this.data.id
    let that = this
    that.hideMarker()
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
            // 用户点击了确定 可以调用删除方法了
            that.deleteAction(id)
          }
        }
      })
  },
  deleteAction(id){
    API.deleteCommodity(id).then(res => {
      wx.showToast({
        icon: 'success',
        title: '删除成功',
        duration: 1500
      });
      this.getTypeList()
    })
  },
  eqCodeAction(){
    let pedestalName = this.data.pedestalName
    let pedestalTypeName = this.data.pedestalTypeName
    let id= this.data.id
    // let parmas="id="+app.globalData.userInfo.id+",pedestalName="+pedestalName+"("+pedestalTypeName+")"
    let parmas="a="+app.globalData.userInfo.id+",b="+id
    let postBody={
      "page": "/pages/index/index",
      "scene": parmas,
      "width": "400"}
      this.hideMarker()
      wx.showToast({
        title: '生成中',
        icon: 'loading'
      })
    API.eqCode(id, postBody).then(res => {
      this.setData({
        imgCode: res.data,
        isImgModal: true
      })
      // wx.showToast({
      //   icon: 'success',
      //   title: '删除成功',
      //   duration: 1500
      // });
      // this.getTypeList()
    })
  }
})